﻿function AttachEvents(container)
{
    $(".Date", container).datepicker(
    {
        dateFormat: 'yy-mm-dd'
    }).each(function ()
    {
        $(this).datepicker("setDate", new Date());
    });

    $('.Numeric', container).blur(function (event)
    {
        if (!/^(\d+(\.\d+)?)?$/.test($(this).val()))
            $(this).val(0);
    });

    var el = $('#OriginatedFromID', container);
    el.ddl({ events: false});
    el.ddl('ddlEl').click(function ()
    {
        var table = new ProjectTable();
        table.show(baseUrl, true, function (val)
        {
            $('#OriginatedFromID').html(stringFormat("<option value='{0}'>{1}</option>", val.id, val.name));
            $('#OriginatedFromID', container).ddl('set');
        });

        table.select(el.val());
    });
    if (user && user.ID)
        $('#AssignedTo').html(stringFormat("<option value='{0}'>{1}</option>", user.ID.Value, user.Name))
    $('#AssignedTo', container).searchForm({type:"User", baseUrl:baseUrl, filter:"userFilter", pName: "Manager"});

    $('.EntitySearch', container).each(function ()
    {
        $(this).searchForm({ type: $(this).attr('type_name'), baseUrl: baseUrl, pName: $(this).closest(".Property").find(".Label").text() });
    });

    $('select').each(function ()
    {
        var self = $(this);
        if (!self.data('loaded'))
            self.ddl();
    });

    var ddls = $('a.PSelect');
    ddls.each(function (idx, el) { $(el).css('z-index', ddls.length - idx); });
}

var initPage = function () {

    $('#btnSubmit, #btnFinish').click(function () {
        var button = this;
        var data;

        if (typeof (validateNew) != "undefined" && !validateNew())
            return;

        if (typeof (getData) != "undefined")
            data = getData();
        else {
            var elements = $('input, select, textarea', $('.CreateForm'));
            data = elements.serialize();
        }

        $('.CreateForm').show_loading(true);

        remoteRequest({
            url: createUrl,
            contentType: "JSON",
            type: "POST",
            data: data,
            success: function (res) {
                $('.CreateForm').show_loading(false);
                if (res.Data) {
                    alert("The record has been created.");
                    if (button.id == "btnSubmit")
                        ClearFields();
                    else {

                        try {
                            if (typeof GetInfo != "undefined" && GetInfo())
                                CloseFloatingWindow();
                            else window.location.href = tasksURL;
                        }
                        catch (er) {
                            window.location.href = tasksURL;
                        }
                    }
                }
                else if (res.ErrorMessage) {
                    alert(res.ErrorMessage);
                    $('.CreateForm, .HeaderContainer').css("zoom", 0.1);
                    $('.CreateForm, .HeaderContainer').css("zoom", 1);
                }
                else alert("error");
            }
        });
    });

    function ClearFields() {
        $('[type="text"], select, textarea', $('.CreateForm')).val('').ddl('set');
        $('[type="checkbox"]').attr('checked', false);
        $('.RemoveOnSave').remove();
    }

    $('#btnCancel').click(function () {
        try {
            if (typeof GetInfo != "undefined" && GetInfo())
                CloseFloatingWindow();
            else window.location.href = tasksURL;
        }
        catch (er) {
            window.location.href =tasksURL;
        }
    });

    AttachEvents();
};

initPage();
$("#Title").focus();